<?
class lego_form extends lego_abstract{
    private $table_name;
	public $form;
	public function __construct($name, $table_name = false, $id = false, $primary_key = 'id'){
		parent::__construct($name);
		$this->form = new view_form($this->getName(), $this->actUri("save")->url());
        if($table_name) $this->createFromTable($table_name, $id, $primary_key);
	}
	
    public function getDir(){return dirname(__FILE__);}

	public function getForm(){
		return $this->form;
	}
    
    public function createFromTable($table_name, $id = false, $primary_key = 'id'){
        $this->table_name = $table_name;
        $this->form->createFromTable($table_name);
        if($id){
            $row = Database::select("select * from `{$table_name}` where `{$primary_key}`='$id'", true);
            if($row){
                $this->form->loadFromArray($row);
            }
        }
    }
    
    public function action_index(){
        return $this->form->getOutput();
    }
	
	public function action_save(){
        if(empty($_POST))  $this->_goto($this->actUri("index")->url());
	    $o = new tbl_abstract(false, $this->table_name);
        $o->loadFromArray($_POST);
        $o->replace();
        $out = "Вставлено<br>";
        $this->form->loadFromArray($_POST);
        $out .= $this->action_index();
        return $out;
	}
}
?>